<?php 
/**
 * 
 * @author tangbl
 *
 */
class CustomModel extends Model {
	
	public function import($fileurl,$cityid){
		//菜品分类对应表
		$fieldname = array(
			0 => array('手机号码',     'mobile'),
			1 => array('姓名',         'realname'),
			2 => array('街道',         'street'),
			3 => array('门牌号',       'menpai'),
			4 => array('交叉路口',     'lukou'),
			5 => array('送餐说明',     'shuoming'),
			6 => array('备注',         'beizhu'),
			7 => array('最后订餐时间', 'last_order'),
		);
		
		vendor('PHPExcel.PHPExcel');
		
		$objReader = PHPExcel_IOFactory::createReader(Excel5);
		$objReader->setReadDataOnly(true);
		$objPHPExcel = $objReader->load($fileurl);
		$objWorksheet = $objPHPExcel->getActiveSheet();
		$highestRow = $objWorksheet->getHighestRow();
		$highestColumn = $objWorksheet->getHighestColumn();
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
		$excelData = array();
		
		$addnum = 0;
		for ($row = 2; $row <= $highestRow; $row++) {
			$model = M('Member');
			$array = "";
			
			$array['addtime']  = time();
			$array['solevar']  = create_id();
			$array['isreg']    = 2;
			$array['neibu']    = 1;
			$array['city']     = $cityid;
			for ($col = 0; $col < $highestColumnIndex; $col++) {
				$value =(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
				$array[$fieldname[$col][1]] = $value;
				
				if($col==7){
					$array['last_order'] = strtotime($value);
				}
				if($col==1){
					$array['realname'] = $value;
				}
				$array['remark'] = $array['street'].'，'.$array['menpai'].'，';
				if($array['lukou']){
					$array['remark'] .= "（交叉路口：".$array['lukou']."）";
				}
				if($array['shuoming']){
					$array['remark'] .= "（送餐说明：".$array['shuoming']."）";
				}
				if($array['beizhu']){
					$array['remark'] .= "（备注：".$array['beizhu']."）";
				}
			}
			if($array['mobile']){
				$where['mobile'] = $array['mobile'];
				$one = $model->where($where)->field('id')->find();
				
				if(!$one['id']){
					$id = $model->add($array);
						
					//保存账号
					$map['id'] = $id;
					$map['username'] = 'kkr'.$id.'@kkrabbit.com';
					$map['email']    = 'kkr'.$id.'@kkrabbit.com';
					$model->save($map);
						
					$addnum++;
				}
			}
		}
		
		$ok['status'] = 1;
		$ok['addnum'] = $addnum;
		
		return $ok;
	}
}